package com.newly.common.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.newly.common.base.exception.NewlyException;

/**
 * @program: newly-parent
 * @remark: xml工具类
 * @author: guan
 * @create: 2022-01-08 11:40
 */
public class XmlUtils {

    /**
     * 对象转换xml字符串
     *
     * @param obj 对象数据
     * @return xml字符串
     */
    public static String objToXmlStr(Object obj) {
        if (ObjectUtil.isEmpty(obj)) return null;
        XmlMapper xmlMapper = new XmlMapper();
        String xmlStr = null;
        try {
            xmlStr = xmlMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new NewlyException("Object failed to covert XML string!");
        }
        return xmlStr;
    }

    /**
     * xml字符串转换对象
     *
     * @param xmlStr xml字符串
     * @return xml字符串
     */
    public static String objToXmlStr(String xmlStr) {
        return null;
    }
}
